package com.song.user.controller;

import com.song.user.pojo.User;
import com.song.user.service.UserService;
import entity.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 宋伟宁
 * @Date 2024/1/5
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/")
    public Result findUser() {
        return new Result(200, "查询成功", true, this.userService.findAll());
    }

    @PostMapping("/login")
    public Result login(String username, String password) {
        User login = this.userService.login(username, password);
        return login == null ? new Result(300, "用户名或密码错误", false)
                : new Result(200, "登录成功", true, login);
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return new Result(200, "保存成功", true, this.userService.save(user));
    }

    @GetMapping("/hasName/{name}")
    public Result hasName(@PathVariable("name") String name) {
        int m =this.userService.findUserByUsername(name);
         return  m==1 ? new Result(200, "查询成功", true)
                 : new Result(200,"查询成功",false);
    }

}
